home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////
- // Pocket PC Game Programming
- // Chapter 9: Sprites and Animation
- //
- // CBitmap Source File
- //
- // This file includes the CBitmap class implementation.
- //
- //////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "CBitmap.h"
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap Constructor
- //
- // This function is called when the class is instantiated.
- //
-
- CBitmap::CBitmap(HDC hdc)
- {
- hScreenDC = hdc;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap Destructor
- //
- // This function is called when the class is terminated.
- //
- CBitmap::~CBitmap()
- {
- if (lpDIB != NULL)
- free(lpDIB);
-
- if (hSourceDC != NULL)
- {
- SelectObject(hSourceDC, hOldBitmap);
- DeleteObject(hOldBitmap);
- DeleteDC(hSourceDC);
- }
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::Create
- //
- // Used to create a new memory device context.
- //
- BOOL CBitmap::Create(int Width, int Height)
- {
- HBITMAP hSourceBitmap;
-
- hSourceDC = CreateCompatibleDC(GetScreenDC());
- if (hSourceDC == NULL)
- return FALSE;
-
- hSourceBitmap = CreateCompatibleBitmap(GetScreenDC(), Width, Height);
- if (hSourceBitmap == NULL)
- return FALSE;
-
- hOldBitmap = SelectBitmap(hSourceDC, hSourceBitmap);
- if (hOldBitmap == NULL)
- return FALSE;
-
- DeleteObject(hSourceBitmap);
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::Load
- //
- // Loads a bitmap file into a device context.
- //
- BOOL CBitmap::Load(LPTSTR lpFilename)
- {
- LPBYTE lpSourceBits;
- HBITMAP hSourceBitmap;
- DWORD dwSourceBitsSize;
- LPBITMAPINFO lpSrcDIB;
-
- SetFilename(lpFilename);
- if (!LoadDIB(lpFilename))
- return FALSE;
-
- lpSrcDIB = (LPBITMAPINFO)lpDIB;
-
- hSourceBitmap = CreateDIBSection(GetScreenDC(), lpSrcDIB, DIB_RGB_COLORS,
- (void **)&lpSourceBits, NULL, 0);
-
- //hSourceDC = CreateCompatibleDC(GetDC(NULL));
- hSourceDC = CreateCompatibleDC(GetScreenDC());
- dwSourceBitsSize = lpSrcDIB->bmiHeader.biHeight *
- BytesPerLine(&(lpSrcDIB->bmiHeader));
- memcpy(lpSourceBits, BitmapDataIndex((LPSTR)lpSrcDIB), dwSourceBitsSize);
-
- SelectObject(hSourceDC, hSourceBitmap);
-
- //store bitmap header into bmBitmap
- GetObject(hSourceBitmap, sizeof(BITMAP), &bmBitmap);
-
- DeleteObject(hSourceBitmap);
-
- return TRUE;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::LoadDIB
- //
- // Reads a bitmap from disk and converts it to a DIB buffer.
- //
- BOOL CBitmap::LoadDIB(LPCTSTR szFileName)
- {
- HANDLE hFile;
- BITMAPFILEHEADER bfh;
- LPBYTE lpTemp = NULL;
- WORD wPaletteSize = 0;
- DWORD dwBitsSize = 0;
- BOOL bRet;
-
- //open the file
- hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- return FALSE;
- }
-
- //read the header
- bRet = ReadFile(hFile, &bfh, sizeof(BITMAPFILEHEADER), &dwImageSize, NULL);
- if ((bRet == FALSE) || (dwImageSize != sizeof(BITMAPFILEHEADER)))
- {
- CloseHandle(hFile);
- return FALSE;
- }
-
- //is this a valid bitmap file?
- if ((bfh.bfType != 0x4d42) || (bfh.bfReserved1 != 0) || (bfh.bfReserved2 != 0))
- {
- CloseHandle(hFile);
- return FALSE;
- }
-
- //allocate memory for the bitmap data
- lpDIB = (LPBYTE) malloc(sizeof(BITMAPINFO));
- if (lpDIB == NULL)
- {
- CloseHandle(hFile);
- return FALSE;
- }
-
- //read in the BITMAPINFOHEADER
- bRet = ReadFile(hFile, lpDIB, sizeof(BITMAPINFOHEADER), &dwImageSize, NULL);
- if ((bRet == FALSE) || (dwImageSize != sizeof(BITMAPINFOHEADER)))
- {
- CloseHandle(hFile);
- free(lpDIB);
- return FALSE;
- }
-
- if (((LPBITMAPINFOHEADER)lpDIB)->biSize != sizeof(BITMAPINFOHEADER))
- {
- CloseHandle(hFile);
- free(lpDIB);
- return FALSE;
- }
-
- wPaletteSize = PaletteSize((LPSTR)lpDIB);
- dwBitsSize = ImageHeight() * BytesPerLine((LPBITMAPINFOHEADER) lpDIB);
-
- //realloc the DIB buffer
- lpTemp = (LPBYTE) realloc(lpDIB, sizeof(BITMAPINFOHEADER) + wPaletteSize + dwBitsSize);
- if (lpTemp == NULL)
- {
- CloseHandle(hFile);
- free(lpDIB);
- return FALSE;
- }
-
- lpDIB = lpTemp;
-
- //read palette if one is present
- if (wPaletteSize > 0)
- {
- bRet = ReadFile(hFile, ((LPBITMAPINFO)lpDIB)->bmiColors, wPaletteSize, &dwImageSize, NULL);
- if ((bRet == FALSE) || (dwImageSize != wPaletteSize))
- {
- CloseHandle(hFile);
- free(lpDIB);
- return FALSE;
- }
- }
-
- if (bfh.bfOffBits > 0)
- {
- if (SetFilePointer(hFile, bfh.bfOffBits, NULL, FILE_BEGIN) == 0xffffffff)
- {
- CloseHandle(hFile);
- free(lpDIB);
- return FALSE;
- }
- }
-
- //read the image data
- bRet = ReadFile(hFile, BitmapDataIndex((LPSTR)lpDIB), dwBitsSize, &dwImageSize, NULL);
- if ((bRet == FALSE) || (dwImageSize != dwBitsSize))
- {
- CloseHandle(hFile);
- free(lpDIB);
- return FALSE;
- }
-
- //close file and return
- CloseHandle(hFile);
- return TRUE;
- }
-
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::BitBlit
- //
- // Blits a bitmap to the default device context.
- //
- BOOL CBitmap::BitBlit(int x, int y)
- {
- return BitBlt(GetScreenDC(), x, y, ImageWidth(), ImageHeight(), hSourceDC, 0, 0, SRCCOPY);
- }
-
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::BitBlit (Overloaded)
- //
- // Blits a bitmap to a destination device context.
- //
- BOOL CBitmap::BitBlit(HDC hdc, int x, int y)
- {
- return BitBlt(hdc, x, y, ImageWidth(), ImageHeight(), hSourceDC, 0, 0, SRCCOPY);
- }
-
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::TransBlit
- //
- // Blits a transparent bitmap to the default device context.
- //
- BOOL CBitmap::TransBlit(int x, int y, COLORREF clrTrans)
- {
- return TransparentImage(GetScreenDC(), x, y, ImageWidth(), ImageHeight(), hSourceDC, 0, 0,
- ImageWidth(), ImageHeight(), clrTrans);
- }
-
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::StretchBlit
- //
- // Blits a scaled bitmap to the default device context.
- //
- BOOL CBitmap::StretchBlit(int x, int y, int dx, int dy)
- {
- return StretchBlt(GetScreenDC(), x, y, dx, dy, hSourceDC, 0, 0, ImageWidth(), ImageHeight(), SRCCOPY);
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::BitmapDataIndex
- //
- // Locate image bits and return a pointer to the memory block
- //
- LPSTR CBitmap::BitmapDataIndex(LPSTR lpbi)
- {
- return (lpbi + *(LPDWORD)lpbi + PaletteSize(lpbi));
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::PaletteSize
- //
- // Return the byte size of the color table.
- //
- int CBitmap::PaletteSize(LPSTR lpbi)
- {
- int iNumColors;
- DWORD dwClrUsed;
-
- dwClrUsed = ((LPBITMAPINFOHEADER) lpbi)->biClrUsed;
- if (dwClrUsed)
- {
- iNumColors = (WORD)dwClrUsed;
- }
- else
- {
- //only applicable to 8-bit or less bit depth
- switch (BitCount())
- {
- case 1:
- iNumColors = 2;
- break;
-
- case 2:
- iNumColors = 4;
- break;
-
- case 4:
- iNumColors = 16;
- break;
-
- case 8:
- iNumColors = 256;
- break;
-
- default:
- iNumColors = 0;
- }
- }
-
- return (iNumColors * sizeof(RGBQUAD));
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::BytesPerLine
- //
- // Returns the number of bytes in one scan line of the bitmap.
- //
- int CBitmap::BytesPerLine(LPBITMAPINFOHEADER lpBMIH)
- {
- return (int)(((ImageWidth() * NumPlanes() * BitCount() + 31) / 32) * 4);
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::BitCount
- //
- // Returns the bit count (color depth) of the bitmap.
- //
- int CBitmap::BitCount()
- {
- return (int)((LPBITMAPINFOHEADER) lpDIB)->biBitCount;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::ImageWidth
- //
- // Returns the horizontal resolution of the bitmap.
- //
- int CBitmap::ImageWidth()
- {
- return (int)((LPBITMAPINFOHEADER) lpDIB)->biWidth;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::ImageHeight
- //
- // Returns the vertical resolution of the bitmap.
- //
- int CBitmap::ImageHeight()
- {
- return (int)((LPBITMAPINFOHEADER) lpDIB)->biHeight;
- }
-
- //////////////////////////////////////////////////////////////////////
- // CBitmap::NumPlanes
- //
- // Returns the number of color planes in the bitmap.
- //
- int CBitmap::NumPlanes()
- {
- return (int)((LPBITMAPINFOHEADER) lpDIB)->biPlanes;
- }
-